<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="950px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-card class="!border-none" shadow="never">
                <el-form ref="formRef" label-width="138px" :model="queryParams" :inline="true">
                    <el-form-item label="案件类型" prop="caseType">
                        <el-select class="w-[690px]" v-model="queryParams.caseType" clearable>
                            <el-option label="专利" value="1" />
                            <el-option label="商标" value="2" />
                            <el-option label="版权" value="3" />
                            <el-option label="科技服务" value="4" />
                            <el-option label="法律服务" value="5" />
                            <el-option label="调查案" value="6" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="我方文号" prop="ourDocumentNumber">
                        <el-input class="w-[690px]" v-model="queryParams.ourDocumentNumber" />
                    </el-form-item>
                    <el-form-item label="申请号" prop="applicationNumber">
                        <el-input class="w-[690px]" v-model="queryParams.applicationNumber" />
                    </el-form-item>
                    <el-form-item style="float: right">
                        <el-button type="primary" @click="resetPage">查询</el-button>
                        <el-button type="primary" @click="resetParams">重置</el-button>
                        <el-button type="primary" @click="resetParams">设置</el-button>
                        <el-button type="primary" @click="resetParams">一键设置</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
            <el-card class="!border-none mt-4" shadow="never">
                <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                    <el-table-column type="selection" width="55" />
                    <!-- <el-table-column label="ID" prop="id" min-width="50" /> -->
                    <el-table-column label="我方文号" prop="ourDocumentNumber" min-width="100" />
                    <el-table-column label="申请号" prop="applicationNumber" min-width="100" />
                    <el-table-column label="案件名称" prop="caseName" min-width="100" />
                    <el-table-column label="案件类型" prop="caseType" min-width="100" />
                    <el-table-column label="业务类型" prop="businessType" min-width="100" />
                    <el-table-column label="申请类型" prop="applicationType" min-width="100" />
                    <el-table-column label="申请国家" prop="designatedCountry" min-width="100" />
                    <el-table-column label="业务人员" prop="businessPersonnel" min-width="100" />
                </el-table>
                <div class="flex justify-end mt-4">
                    <pagination v-model="pager" @change="getLists" />
                </div>
            </el-card>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { clientcaseEdit, clientcaseAdd, clientcaseDetail } from '@/api/clientcase'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑客户案件' : '设置案件第一联系人'
})

const formData = reactive({
    id: '',
    publicationNumber: '',
    contractNo: '',
    contractName: '',
    customerDocumentNumber: '',
    applicantId: '',
    applicant: '',
    ourDocumentNumber: '',
    applicationNumber: '',
    applicationDate: '',
    caseName: '',
    caseTypeId: '',
    caseType: '',
    caseStateId: '',
    caseState: '',
    businessTypeId: '',
    businessType: '',
    applicationTypeId: '',
    applicationType: '',
    designatedCountry: '',
    firstContact: '',
    openingDate: '',
    businessPersonnelId: '',
    businessPersonnel: '',
    originalSalesmanId: '',
    originalSalesman: '',
    undertakeDepartmentId: '',
    undertakeDepartment: '',
    processPersonnelId: '',
    processPersonnel: ''
})

const formRules = {}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await clientcaseEdit(data) : await clientcaseAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await clientcaseDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})

import { clientcaseDelete, clientcaseLists } from '@/api/clientcase'
import { usePaging } from '@/hooks/usePaging'
const queryParams = reactive({
    publicationNumber: '',
    contractNo: '',
    customerDocumentNumber: '',
    applicant: '',
    ourDocumentNumber: '',
    applicationNumber: '',
    caseName: '',
    caseType: '',
    openingDateStart: '',
    openingDateEnd: ''
})
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: clientcaseLists,
    params: queryParams
})
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await clientcaseDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
